package com.itfreer.power.service.useredit;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.itfreer.form.api.BaseDao;
import com.itfreer.form.api.imp.DictionaryConvertServiceImp;
import com.itfreer.power.dao.useredit.UserEditApplyDao;
import com.itfreer.power.entity.jurisdiction.RegisterServiceEntity;
import com.itfreer.power.entity.user.UserInfoEntity;
import com.itfreer.power.entity.useredit.UserEditApplyEntity;
import com.itfreer.power.entity.useredit.UserEditApplyItemEntity;
import com.itfreer.power.entity.useredit.UserSysAuthoEntity;
import com.itfreer.power.service.jurisdiction.RegisterServiceService;
import com.itfreer.power.service.power.PowerService;
import com.itfreer.power.service.user.UserInfoService;

/**
 * 用户申请服务
 */
@Component("UserEditApplyServiceBase")
public class UserEditApplyServiceBase extends DictionaryConvertServiceImp<UserEditApplyEntity>
		implements UserEditApplyService {

	@Autowired
	private UserEditApplyDao userEditApplyDao;
	
	@Autowired(required = false)
	private UserInfoService userInfoService;
	
	@Autowired(required = false)
	private UserSysAuthoService userSysAuthoService;
	
	@Autowired(required = false)
	private PowerService powerService;
	
	@Autowired(required = false)
	private RegisterServiceService  registerServiceService;
 
	@Override
	protected BaseDao<UserEditApplyEntity> getDao() {
		return userEditApplyDao;
	}

	/**
	 * 用户申请新增保存，默认申请状态为草稿
	 * 申请状态state  1、草稿   2、待审核    3、审核通过    4、审核不通过
	 */
	@Override
	public UserEditApplyEntity add(UserEditApplyEntity entity) {
		
		//保存默认申请状态为草稿
		if("".equals(entity.getState()) || entity.getState()==null) {
			entity.setState("1");
		};
		
		//审核通过逻辑处理
		if("3".equals(entity.getState())) {   //审核通过
			Set<UserEditApplyItemEntity> listUser = entity.getItems();
			UserSysAuthoEntity usersysyEntity = new UserSysAuthoEntity();
			for (UserEditApplyItemEntity user : listUser) {
				
				//修改类型为新增
				if("1".equals(user.getApplyType())) {
					UserInfoEntity entityUser = new UserInfoEntity();
					entityUser.setUserid(user.getUserid());						//用户id
					entityUser.setUserName(user.getUserName());					//用户名
					entityUser.setOrganizationId(user.getOrganizationId());		//机构
					entityUser.setDepartmentId(user.getDepartmentId());			//部门
					entityUser.setTel(user.getTel());							//电话号码
					userInfoService.add(entityUser);
					
					if(user.getRegisterService() != null || !"".equals(user.getRegisterService())) {
						String[] arr = user.getRegisterService().split(",");  //申请访问的系统    
						List<String> applyAccess = new ArrayList<String>();  //String数组转换成list
						Collections.addAll(applyAccess, arr);	
						//新增用户为新增的，全部都没有访问权限，不需要特殊处理
						for (String apply : applyAccess) {
							usersysyEntity.setUserid(user.getUserid());     //用户id
							usersysyEntity.setRegisterServiceId(apply);     //系统id
							usersysyEntity.setAuthorizationType("1");
							userSysAuthoService.add(usersysyEntity);
						}	
					}
		
				};	
				//修改类型为修改
				if("2".equals(user.getApplyType())) {
					UserInfoEntity entityUser = userInfoService.getEntity(user.getUserid());
					//entityUser.setUserid(user.getUserid());
					entityUser.setUserName(user.getUserName());
					entityUser.setOrganizationId(user.getOrganizationId());
					entityUser.setDepartmentId(user.getDepartmentId());
					entityUser.setTel(user.getTel());	
					userInfoService.update(entityUser);
					
					//通过userid获取角色集
					List<String> sysRoleList = powerService.getUserRoles(user.getUserid());
					//通过角色集获取可以访问的系统集
					List<String> accessibleSys = powerService.getRoleService(sysRoleList);
					
					List<RegisterServiceEntity> registerServiceEntity = registerServiceService.getEntitys(null, null, null, 100, 1);
					List<String> registerServiceId = new ArrayList<String>();   //所有系统id
					for (RegisterServiceEntity registerService : registerServiceEntity) {
						String id = registerService.getId();
						registerServiceId.add(id);
					}
						
					if(user.getRegisterService() != null || !"".equals(user.getRegisterService())) {
						
						String[] arr = user.getRegisterService().split(",");  //申请访问的系统    
						
						List<String> applyAccess = new ArrayList<String>();  //String数组转换成list
						Collections.addAll(applyAccess, arr);	
						List<String> cancelAccess = new ArrayList<String>();         //取消申请访问的系统 
						List<String> noAccessibleSys = new ArrayList<String>();  //不能访问的系统
						for (String sysAll : registerServiceId) { 
							if(applyAccess.contains(sysAll)) {
								
							}else {
								cancelAccess.add(sysAll);
							};
							
							if(accessibleSys.contains(sysAll)) {
								
							}else {
								noAccessibleSys.add(sysAll);
							};
						};
						//申请访问的系统中找出没有访问权限的系统，存入数据库，授权类型为授权
						for (String apply : applyAccess) {
							if(noAccessibleSys.contains(apply)) {
								usersysyEntity.setSysId(UUID.randomUUID().toString().replace("-", ""));
								usersysyEntity.setUserid(user.getUserid());     //用户id
								usersysyEntity.setRegisterServiceId(apply);     //系统id
								usersysyEntity.setAuthorizationType("1");
								userSysAuthoService.add(usersysyEntity);
							}
						}
						//取消申请访问的系统，找出有访问权限的，存入数据库，授权类型为取消授权
						for (String cance : cancelAccess) {
							if(accessibleSys.contains(cance)) {
								usersysyEntity.setSysId(UUID.randomUUID().toString().replace("-", ""));
								usersysyEntity.setUserid(user.getUserid());     
								usersysyEntity.setRegisterServiceId(cance);     
								usersysyEntity.setAuthorizationType("2");
								userSysAuthoService.add(usersysyEntity);
							}
						}			
					}
					
				};
				//修改类型为删除
				if("3".equals(user.getApplyType())) {
					userInfoService.delete(user.getUserid());
					//userSysAuthoService.delete("");               //删除系统访问表中的记录
				};	
			}
		};
		
		return super.add(entity);
	}

	/**
	 * 用户申请修改保存，默认申请状态为草稿
	 */
	@Override
	public UserEditApplyEntity update(UserEditApplyEntity entity) {
		
		if("".equals(entity.getState()) || entity.getState()==null) {
			entity.setState("1");
		};
		
		if("3".equals(entity.getState())) {   //审核通过
			Set<UserEditApplyItemEntity> listUser = entity.getItems();
			UserSysAuthoEntity usersysyEntity = new UserSysAuthoEntity();
			for (UserEditApplyItemEntity user : listUser) {
				
				//修改类型为新增
				if("1".equals(user.getApplyType())) {
					UserInfoEntity entityUser = new UserInfoEntity();
					entityUser.setUserid(user.getUserid());						//用户id
					entityUser.setUserName(user.getUserName());					//用户名
					entityUser.setOrganizationId(user.getOrganizationId());		//机构
					entityUser.setDepartmentId(user.getDepartmentId());			//部门
					entityUser.setTel(user.getTel());							//电话号码
					userInfoService.add(entityUser);

				};	
				//修改类型为修改
				if("2".equals(user.getApplyType())) {
					UserInfoEntity entityUser = userInfoService.getEntity(user.getUserid()); //不用new是为了不删除字表信息
					entityUser.setUserid(user.getUserid());
					entityUser.setUserName(user.getUserName());
					entityUser.setOrganizationId(user.getOrganizationId());
					entityUser.setDepartmentId(user.getDepartmentId());
					entityUser.setTel(user.getTel());
					userInfoService.update(entityUser);
					
					//通过userid获取角色集
					List<String> sysRoleList = powerService.getUserRoles(user.getUserid());
					//通过角色集获取可以访问的系统集
					List<String> accessibleSys = powerService.getRoleService(sysRoleList);
					
					List<RegisterServiceEntity> registerServiceEntity = registerServiceService.getEntitys(null, null, null, 100, 1);
					List<String> registerServiceId = new ArrayList<String>();   //所有系统id
					for (RegisterServiceEntity registerService : registerServiceEntity) {
						String id = registerService.getId();
						registerServiceId.add(id);
					}
						
					if(user.getRegisterService() != null || !"".equals(user.getRegisterService())) {
						
						String[] arr = user.getRegisterService().split(",");  //申请访问的系统    
						
						List<String> applyAccess = new ArrayList<String>();  //String数组转换成list
						Collections.addAll(applyAccess, arr);	
						List<String> cancelAccess = new ArrayList<String>();     //取消申请访问的系统 
						List<String> noAccessibleSys = new ArrayList<String>();  //不能访问的系统
						for (String sysAll : registerServiceId) { 
							if(applyAccess.contains(sysAll)) {
								
							}else {
								cancelAccess.add(sysAll);
							};
							
							if(accessibleSys.contains(sysAll)) {
								
							}else {
								noAccessibleSys.add(sysAll);
							};
						};
						//申请访问的系统中找出没有访问权限的系统，存入数据库，授权类型为授权
						for (String apply : applyAccess) {
							if(noAccessibleSys.contains(apply)) {
								usersysyEntity.setSysId(UUID.randomUUID().toString().replace("-", ""));
								usersysyEntity.setUserid(user.getUserid());     //用户id
								usersysyEntity.setRegisterServiceId(apply);     //系统id
								usersysyEntity.setAuthorizationType("1");
								userSysAuthoService.add(usersysyEntity);
							}
						}
						//取消申请访问的系统，找出有访问权限的，存入数据库，授权类型为取消授权
						for (String cance : cancelAccess) {
							if(accessibleSys.contains(cance)) {
								usersysyEntity.setSysId(UUID.randomUUID().toString().replace("-", ""));
								usersysyEntity.setUserid(user.getUserid());     
								usersysyEntity.setRegisterServiceId(cance);     
								usersysyEntity.setAuthorizationType("2");
								userSysAuthoService.add(usersysyEntity);
							}
						}			
					}
				};
				//修改类型为删除，同时删除此用户和角色的挂接
				if("3".equals(user.getApplyType())) {
					userInfoService.delete(user.getUserid());
				};	
			}
		};
		return super.update(entity);
	}
	
}